home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / art&graf.ix / art-0015 / flicker / files.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  10KB  |  526 lines

  1.  
  2. #include <osbind.h>
  3. #include <gemdefs.h>
  4. #include "flicker.h"
  5.  
  6.  
  7. extern WORD *cscreen;
  8. extern WORD current_drive;
  9. static struct neo_head n;
  10. static struct degas_head d;
  11.  
  12. gmessage(s)    /* message with gem cursor ... the hassles of keeping the
  13.             cursor visible during file io ... especially when you use
  14.             a different cursor than GEM does */
  15. char *s;
  16. {
  17. ghide_mouse();
  18. top_print(s);
  19. gshow_mouse();
  20. }
  21.  
  22. gtop_line(s)
  23. char *s;
  24. {
  25. ghide_mouse();
  26. top_print(s);
  27. gshow_mouse();
  28. wait_ednkey();
  29. }
  30.  
  31. checking_dfree(needs)
  32. register long needs;
  33. {
  34. register long dfree;
  35. long buf[4];
  36. char cbuf[40];
  37.  
  38. sprintf(cbuf, "checking disk for %ld free bytes", needs);
  39. gmessage(cbuf);
  40. Dfree(buf, current_drive+1);
  41. dfree = buf[0] * buf[2] * buf[3];
  42. sprintf(cbuf, "%ld free bytes need %ld", dfree, needs);
  43. gmessage(cbuf);
  44. return( dfree >= needs );
  45. }
  46.  
  47.  
  48. file_truncated(name)
  49. char *name;
  50. {
  51. char buf[50];
  52.  
  53. sprintf(buf, "file %s truncated", name);
  54. gtop_line(buf);
  55. }
  56.  
  57. couldnt_open(name)
  58. char *name;
  59. {
  60. char buf[50];
  61.  
  62. sprintf(buf, "couldn't open %s", name);
  63. gtop_line(buf);
  64. }
  65.  
  66. load_pic(name)
  67. register char *name;
  68. {
  69. register WORD fd;
  70. WORD success = 0;
  71.  
  72. if ((fd = Fopen(name, 0)) < 0)
  73.     {
  74.     couldnt_open(name);
  75.     goto loaded_pic;
  76.     }
  77. if (degas)
  78.     {
  79.     if ( Fread(fd, (long)sizeof(d), &d) < sizeof(d) )
  80.         {
  81.         file_truncated(name);
  82.         goto loaded_pic;
  83.         }
  84.     }
  85. else
  86.     {
  87.     if ( Fread(fd, (long)sizeof(n), &n) < sizeof(n) )
  88.         {
  89.         file_truncated(name);
  90.         goto loaded_pic;
  91.         }
  92.     }
  93. if (Fread(fd, (long)32000, screens[screen_ix]) < 32000)
  94.     {
  95.     file_truncated(name);
  96.     goto loaded_pic;
  97.     }
  98. if (degas)
  99.     put_cmap(d.colormap);
  100. else
  101.     put_cmap(n.colormap);
  102. success = 1;
  103. loaded_pic:
  104. if (fd >= 0)
  105.     Fclose(fd);
  106. return(success);
  107. }
  108.  
  109. save_pic(name)
  110. register char *name;
  111. {
  112. register WORD fd;
  113. WORD success = 0;
  114.  
  115. Fdelete(name);
  116. if ((fd = Fcreate(name, 0)) < 0)
  117.     {
  118.     couldnt_open(name);
  119.     goto saved_pic;
  120.     }
  121. if (degas)
  122.     {
  123.     word_zero(&d, sizeof(d)/sizeof(WORD) );
  124.     copy_words(sys_cmap, d.colormap, 16);
  125.     if ( Fwrite(fd, (long)sizeof(d), &d) < sizeof(d) )
  126.         {
  127.         file_truncated(name);
  128.         goto saved_pic;
  129.         }
  130.     }
  131. else
  132.     {
  133.     word_zero(&n, sizeof(n)/sizeof(WORD) );
  134.     copy_words(sys_cmap, n.colormap, 16);
  135.     n.slide_time = 10;
  136.     n.width = 320;
  137.     n.height = 200;
  138.     if ( Fwrite(fd, (long)sizeof(n), &n) < sizeof(n) )
  139.         {
  140.         file_truncated(name);
  141.         goto saved_pic;
  142.         }
  143.     }
  144. if ( Fwrite(fd, (long)32000, screens[screen_ix]) < 32000 )
  145.     {
  146.     file_truncated(name);
  147.     goto saved_pic;
  148.     }
  149. success = 1;
  150. saved_pic:
  151. if (fd >= 0)
  152.     Fclose(fd);
  153. return(success);
  154. }
  155.  
  156. load_col(name)
  157. register char *name;
  158. {
  159. register int fd;
  160.  
  161. if ((fd = Fopen(name, 0))<0)
  162.     {
  163.     couldnt_open(name);
  164.     goto loaded_col;
  165.     }
  166. if (Fread(fd, (long)32, n.colormap) != 32)
  167.     file_truncated(name);
  168. else
  169.     put_cmap(n.colormap);
  170. loaded_col:
  171. if (fd >= 0)
  172.     Fclose(fd);
  173. return;
  174. }
  175.  
  176. save_col(name)
  177. register char *name;
  178. {
  179. register int fd;
  180.  
  181. Fdelete(name);
  182. if ((fd = Fcreate(name, 0)) < 0)
  183.     {
  184.     couldnt_open(name);
  185.     goto saved_col;
  186.     return;
  187.     }
  188. copy_words(sys_cmap, n.colormap, 16);
  189. if (Fwrite(fd, (long)32, n.colormap) != 32)
  190.     {
  191.     file_truncated(name);
  192.     }
  193. saved_col:
  194. if (fd >= 0)
  195.     Fclose(fd);
  196. return;
  197. }
  198.  
  199.  
  200. load_cel(name)
  201. register char *name;
  202. {
  203. struct neo_head n;
  204. register int fd;
  205. register long length;
  206. register Cel *cel = NULL;
  207.  
  208. free_cel(clipping);
  209. clipping = NULL;
  210. if ((fd = Fopen(name, 0))<0)
  211.     {
  212.     couldnt_open(name);
  213.     goto cleanup_lw;
  214.     }
  215. if (Fread(fd, (long)sizeof(n), &n) != sizeof(n) )
  216.     {
  217.     file_truncated(name);
  218.     goto cleanup_lw;
  219.     }
  220. if ((cel = alloc_cel(n.width, n.height)) == NULL)
  221.     {
  222.     outta_memory();
  223.     goto cleanup_lw;
  224.     }
  225. copy_words(n.colormap, cel->cmap, 16);
  226. cel->xoff = n.xoff;
  227. cel->yoff = n.yoff;
  228.  
  229. if (Fread(fd, (long)cel->image_size, cel->image ) != cel->image_size)
  230.     file_truncated();
  231. if (!mask_cel(cel))
  232.     {
  233.     outta_memory();
  234.     free_cel(cel);
  235.     cel = NULL;
  236.     }
  237. cleanup_lw:
  238. if (fd >= 0)
  239.     Fclose(fd);
  240. clipping = cel;
  241. return;
  242. }
  243.  
  244. save_cel(name)
  245. register char *name;
  246. {
  247. register WORD fd;
  248. register Cel *cel;
  249. WORD success = 0;
  250.  
  251. cel = clipping;
  252. word_zero(&n, sizeof(n)/sizeof(WORD) );
  253. n.type = -1;
  254. n.xoff = cel->xoff;
  255. n.yoff = cel->yoff;
  256. n.width = cel->width;
  257. n.height = cel->height;
  258. n.slide_time = 10;
  259. copy_words(sys_cmap, n.colormap, 16);
  260. Fdelete(name);
  261. if ((fd = Fcreate(name, 0)) < 0)
  262.     {
  263.     couldnt_open(name);
  264.     goto saved_cel;
  265.     }
  266. if ( Fwrite(fd, (long)sizeof(n), &n) < sizeof(n) )
  267.     {
  268.     file_truncated(name);
  269.     goto saved_cel;
  270.     }
  271. if ( Fwrite(fd, (long)cel->image_size, cel->image) < cel->image_size )
  272.     {
  273.     file_truncated(name);
  274.     goto saved_cel;
  275.     }
  276. success = 1;
  277. saved_cel:
  278. if (fd >= 0)
  279.     Fclose(fd);
  280. return(success);
  281. }
  282.  
  283. static long *offsets;
  284.  
  285. static
  286. free_offsets()
  287. {
  288. if (offsets != NULL)
  289.     mfree(offsets, screen_ct*sizeof(long) );
  290. offsets = NULL;
  291. }
  292.  
  293. save_seq(name)
  294. char *name;
  295. {
  296. long dneeds;
  297. Seq_header h;
  298. int i;
  299. WORD width, height;
  300. register WORD maxwidth, maxheight;
  301. register long acc;
  302. long this_size;
  303. long off_size;    /* sizeof offsets */
  304. long cel_size;
  305. WORD success = 0;
  306. WORD *sscreen;
  307. register WORD fd = -1;
  308. register Cel *cel = NULL;
  309.  
  310.  
  311. /* first do some pre-calculations.  Find out how much disk space it will
  312.    take, and while we're at it make up offsets table, so file can be
  313.    used with random access to cels later */
  314. if ((offsets = (long *)alloc(screen_ct*sizeof(long))) == NULL)
  315.     {
  316.     outta_memory();
  317.     goto done_sseq;
  318.     }
  319. maxwidth = maxheight = 0;
  320. dneeds = sizeof(h);    
  321. off_size = screen_ct;
  322. off_size *= 4;
  323. dneeds += off_size;    
  324. acc = dneeds;
  325. dneeds += (long)sizeof(n) * screen_ct;
  326. for (i=0; i<screen_ct; i++)
  327.     {
  328.     offsets[i] = acc;
  329.     if (find_clip(screens[i]))
  330.         {
  331.         width = x_1-x_0;
  332.         height = y_1-y_0;
  333.         if (width > maxwidth)
  334.             maxwidth = width;
  335.         if (height > maxheight)
  336.             maxheight = height;
  337.         this_size = Raster_block(width, height);
  338.         dneeds += this_size;
  339.         acc += this_size;
  340.         acc += sizeof(n);
  341.         }
  342.     else
  343.         {
  344.         acc += sizeof(n);
  345.         }
  346.     }
  347. if ( maxwidth == 0 && maxheight == 0)
  348.     {
  349.     maxwidth = maxheight = 1;
  350.     }
  351. if ( (cel = alloc_cel(maxwidth, maxheight)) == NULL)
  352.     {
  353.     outta_memory();
  354.     goto done_sseq;
  355.     }
  356. Fdelete(name);
  357. if (!checking_dfree(dneeds))
  358.     {
  359.     wait_a_jiffy(100);
  360.     goto done_sseq;
  361.     }
  362. /* ok, now that we've got enough space ...    */
  363. if ((fd = Fcreate(name, 0)) < 0)
  364.     {
  365.     couldnt_open(name);
  366.     goto done_sseq;
  367.     }
  368. word_zero(&h, sizeof(h)/sizeof(WORD) );
  369. h.magic = 0xfedc;    /* write magic number */
  370. h.version = 0;        /* well start at zero I guess */
  371. h.cel_count = screen_ct;
  372.  
  373. /* write out header */
  374. if (Fwrite(fd, (long)sizeof(h), &h) < sizeof(h) )
  375.     {
  376.     file_truncated(name);
  377.     goto done_sseq;
  378.     }
  379.  
  380. /* write out offsets */
  381. if (Fwrite(fd, off_size, offsets) < off_size)
  382.     {
  383.     file_truncated(name);
  384.     goto done_sseq;
  385.     }
  386.  
  387. /* pre-init neo header */
  388. word_zero(&n, sizeof(n)/sizeof(WORD) );
  389. copy_words(sys_cmap, n.colormap, 16);
  390. n.type = -1;
  391. for (i=0; i<screen_ct; i++)
  392.     {
  393.     sscreen = screens[i];
  394.     ghide_mouse();
  395.     copy_screen(sscreen, cscreen);
  396.     gshow_mouse();
  397.     if (find_clip(screens[i]))
  398.         {
  399.         n.width = cel->width = x_1-x_0;
  400.         n.height = cel->height = y_1-y_0;
  401.         n.xoff = cel->xoff = x_0;
  402.         n.yoff = cel->yoff = y_0;
  403.         n.slide_time = 10;
  404.         cel_size = Raster_block(n.width, n.height);
  405.         word_zero(cel->image, (int)cel_size/sizeof(WORD) );
  406.         clip_from_screen(cel, sscreen);
  407.         }
  408.     else
  409.         {
  410.         n.width = n.height = n.xoff = n.yoff = 
  411.             cel->width = cel->height = cel->xoff = cel->yoff = 0;
  412.         n.slide_time = 10;
  413.         cel_size = 0;
  414.         }
  415.     if ( Fwrite(fd, (long)sizeof(n), &n) < sizeof(n) )
  416.         {
  417.         file_truncated(name);
  418.         goto done_sseq;
  419.         }
  420.     if (cel_size > 0)
  421.         {
  422.         if (Fwrite(fd, cel_size, cel->image) < cel_size)
  423.             {
  424.             file_truncated(name);
  425.             goto done_sseq;
  426.             }
  427.         }
  428.     }
  429. success = 1;    /* whew, made it! */
  430.  
  431. done_sseq:
  432. free_offsets();
  433. free_cel(cel);
  434. if (fd >= 0)
  435.     Fclose(fd);
  436. return(success);
  437. }
  438.  
  439. load_seq(name)
  440. char *name;
  441. {
  442. register WORD fd;
  443. Seq_header h;
  444. register WORD i;
  445. long est_mem;
  446. register Cel *cel = NULL;
  447. WORD success = 0;
  448.  
  449. if ((fd = Fopen(name, 0)) < 0)
  450.     {
  451.     couldnt_open(name);
  452.     goto end_lseq;
  453.     }
  454. if ( Fread(fd, (long)sizeof(h), &h) < sizeof(h) )
  455.     {
  456.     file_truncated(name);
  457.     goto end_lseq;
  458.     }
  459. if (h.magic != 0xfedc )
  460.     {
  461.     gtop_line("magic not $fedc!");
  462.     goto end_lseq;
  463.     }
  464. if (h.cel_count > MAX_SCREENS)
  465.     {
  466.     gtop_line("too many screens in file");
  467.     goto end_lseq;
  468.     }
  469. clear_seq();
  470. est_mem = h.cel_count * 32000 + 8000;  /* rude to get memory too low */
  471. if (mem_free < est_mem)
  472.     {
  473.     gtop_line("not enough memory for that file");
  474.     goto end_lseq;
  475.     }
  476. for (i = 1; i<h.cel_count; i++)    /* allocate all the screens it will take */
  477.     {
  478.     if ( (screens[i] = alloc(32000) ) == NULL)
  479.         {
  480.         outta_memory();
  481.         goto end_lseq;
  482.         }
  483.     screen_ct++;
  484.     }
  485. Fseek( h.cel_count * sizeof(long), fd, 1);    /* skip past the offset lists */
  486. for (i=0; i<h.cel_count; i++)
  487.     {
  488.     if ( Fread(fd, (long)sizeof(n), &n) < sizeof(n) )
  489.         {
  490.         file_truncated(name);
  491.         goto end_lseq;
  492.         }
  493.     if (n.width > 0 && n.height > 0)
  494.         {
  495.         if ((cel = alloc_cel(n.width, n.height)) == NULL)
  496.             {
  497.             outta_memory();
  498.             goto end_lseq;
  499.             }
  500.         cel->xoff = n.xoff;
  501.         cel->yoff = n.yoff;
  502.         if (Fread(fd, (long)cel->image_size, cel->image) < cel->image_size)
  503.             {
  504.             file_truncated(name);
  505.             goto end_lseq;
  506.             }
  507.         }
  508.     ghide_mouse();
  509.     put_cmap(n.colormap);
  510.     clear_screen();
  511.     if (cel != NULL)
  512.         copy_celblit(0, 0, cel);
  513.     copy_screen(cscreen, screens[i]);
  514.     gshow_mouse();
  515.     free_cel(cel);
  516.     cel = NULL;
  517.     }
  518. success = 1;
  519.  
  520. end_lseq:
  521. free_cel(cel);
  522. if (fd >= 0)
  523.     Fclose(fd);
  524. return(success);
  525. }
  526.